package vue;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import controller.PlateformeMain;

import frameListeners.FermerFenetre;
import frameListeners.LoadChosenPluginsListener;

@SuppressWarnings("serial")
public class ListOfPluginsFrame extends JFrame {

	private JPanel panel;
	private JMenuItem menuItem;
	
	public ListOfPluginsFrame(JMenuItem m){
		super();
		
		this.menuItem=m;
		this.panel=new JPanel();
		
		this.setTitle("Liste des plugins disponibles"); 
		this.setSize(290,340);
		this.setLocationRelativeTo(null); 
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		this.panel.setBackground(Color.white);
		this.panel.setLayout(new GridBagLayout());
		this.setContentPane(this.panel);
		
		this.buildContent();
		this.open();
	}

	private void buildContent() {
		// TODO Auto-generated method stub
		
		JScrollPane jsp = new JScrollPane();
		jsp.setPreferredSize(new Dimension(284,280));
		jsp.getViewport().setBackground(Color.white);
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=0;
		
		JButton charger = new JButton("Charger les plugins selectionnes");
		charger.setPreferredSize(new Dimension(284,30));
		GridBagConstraints gbc1 = new GridBagConstraints();
		gbc1.gridx=0;
		gbc1.gridy=1;
		gbc.anchor=GridBagConstraints.SOUTH;
		
		JPanel listeOfPlugins = new JPanel();
		listeOfPlugins.setLayout(new BoxLayout(listeOfPlugins, BoxLayout.PAGE_AXIS));
		listeOfPlugins.setBackground(Color.white);
		
		JCheckBox plugin;

		for(String ipp : PlateformeMain.core.getPluginStorage().getNameOfAvailablePlugins()){
			plugin = new JCheckBox();
			plugin.setBackground(Color.white);
			plugin.setText(ipp);
			listeOfPlugins.add(plugin);
		}
		
		jsp.getViewport().add(listeOfPlugins);
		
		charger.addActionListener(new LoadChosenPluginsListener(listeOfPlugins));
		charger.addActionListener(new FermerFenetre(this));
		
		panel.add(jsp,gbc);
		panel.add(charger,gbc1);
	}

	public void open() {
		// TODO Auto-generated method stub
		this.setVisible(true);
	}
	
	@Override
	public void dispose(){
		this.menuItem.setEnabled(true);
		super.dispose();
	}
}
